<template>
    <div class="app-container">
        <div class="form-box">
            <div class="form-group">
                <div class="form-label">文章标题</div>
                <div class="form-input">
                    <el-input v-model="news.title" placeholder="请输入文章标题"></el-input>
                </div>
            </div>

            <div class="form-group">
                <div class="form-label">文章作者</div>
                <div class="form-input">
                    <el-input v-model="news.author" placeholder="请输入文章作者"></el-input>
                </div>
            </div>
            <div class="form-row">
                <div class="form-col">
                    <div class="form-group">
                        <div class="form-label">文章模块</div>
                        <div class="form-input">
                            <el-select v-model="news.moduleId" placeholder="please select your zone" style="width: 100%;">
                                <el-option v-for="m in newsModules" :label="m.name" :value="m.id" :key="m.id"></el-option>
                            </el-select>
                        </div>
                    </div>
                </div>
                <div class="form-col">
                    <div class="form-group">
                        <div class="form-label">发布日期</div>
                        <div class="form-input">
                            <el-date-picker type="date" placeholder="Pick a date" v-model="news.postDate" style="width: 100%;"></el-date-picker>
                        </div>
                    </div>
                </div>
            </div>
            <div class="editor-box">
                <editor :value="news.content" @input="(content)=> news.content = content" :setting="editorSetting"></editor>
            </div>
            <div class="btn-group">
                <el-button  type="primary"  @click="post()">发布</el-button>
            </div>

        </div>



    </div>
</template>

<script>
    import Vue from 'vue'
    import axios from 'axios'
    import '@/styles/form-box.css'
    import editor from '@/components/Editor/index'
    import global from '@/utils/global'
    import newsModuleApi from '@/api/newsModule'
    import newsApi from '@/api/news'
    export default {
        data() {
            return {
                news: {
                    title: '',
                    author: '',
                    postDate: new Date(),
                    delivery: false,
                    content: 'init content',
                    moduleId:null
                },
                newsModules:[],
                editorSetting:{
                    height:250
                }
            }
        },
        mounted:function () {
            const _this = this;
        //    加载newsModules
            newsModuleApi.findAll().then(function (data) {
                _this.newsModules = data
            }).catch(function () {
                _this.$message({type:'error',message:'加载文章模块信息失败'})
            })
        },
        methods: {
            post: function () {
                const _this = this
                newsApi.add(_this.news).then(function (data) {
                    _this.$message({type:'success',message:'发布文章成功!'})
                }).catch(function () {
                    _this.$message({type:'error',message:'发布文章失败!'})
                })
            }
        },
        components: {
            'editor': editor
        }
    }
</script>

<style scoped>

</style>

